package network;

import gui.RiskGUI;

public class ClientThread extends Thread {

	private RiskGUI gui;
	private int meIndex;

	public ClientThread(RiskGUI gui, int meIndex) {
		this.gui = gui;
		this.meIndex = meIndex;
	}

	public void run(){
		while(true){
			NetworkMsg msg = Client.getMsg();
			if(msg == null)
				return;
			//dispatching messages
			switch(msg.getType()){
			case Info:
				InfoMsg m = (InfoMsg)msg;
				System.out.println(m.getInfo());
				break;
			case DBInfo:
				Client.addClientMsg(msg);
				break;
			case Error:
				Client.addClientMsg(msg);
				break;
			case UpdateGameMngr:
				if(gui.getGame().getCurrentPlayer().isMe()){
					break;
				}
				GameMngrMsg mngrMsg = (GameMngrMsg)msg;
				gui.newGameMngr.set(mngrMsg.getGame());
				gui.updateGUI.compareAndSet(false, true);
				gui.addToLog.compareAndSet(null, mngrMsg.getBriefDo());
				RiskGUI.display.wake();
				break;
			case Disconnection:
				DisconnectionMsg disconnection = (DisconnectionMsg)msg;
				gui.playerDisconnect.compareAndSet(null, disconnection.getName());
				return;
			case YouLost:
				if(gui.getGame().getCurrentPlayer().isMe()){
					break;
				} else {
					RiskGUI.lostNetworkGame = true;
				}
			}
		}
	}
}
